/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.contract.entity.ContractPositionMarginDetail;
import com.pig4cloud.pigx.contract.service.ContractPositionMarginDetailService;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import com.pig4cloud.pigx.contract.vo.ContractPositionMarginDetailVO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 仓位保证金变更明细
 *
 * @since 2022/9/12 22:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/contractpositionmargindetail")
@Api(value = "contractpositionmargindetail", tags = "仓位保证金变更明细表管理")
public class ContractPositionMarginDetailController {

	private final ContractPositionMarginDetailService contractPositionMarginDetailService;

	/**
	 * 根据仓位id获取保证金变更明细
	 * @param id 仓位id
	 * @return 1
	 */
	@GetMapping("/position/{id}")
	public R getMarginDetailByPosition(@PathVariable("id") Long id) {
		BaseMapper<ContractPositionMarginDetail> mapper = contractPositionMarginDetailService.getBaseMapper();
		List<ContractPositionMarginDetail> details = mapper.selectList(new QueryWrapper<ContractPositionMarginDetail>() {{
			eq("position_id", id);
			select("operation_type", "margin", "create_time","liquidate_price");
		}});
		List<ContractPositionMarginDetailVO> list = new ArrayList<>();
		details.forEach(e -> {
			ContractPositionMarginDetailVO vo = new ContractPositionMarginDetailVO();
			BeanUtils.copyProperties(e,vo);
			vo.setLiquidatePrice(vo.getLiquidatePrice().setScale(2, BigDecimal.ROUND_HALF_UP));
			vo.setMargin(vo.getMargin().setScale(2, BigDecimal.ROUND_HALF_UP));
			list.add(vo);
		});
		return R.ok(list);
	}

}
